﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZealousTTS.Domain;
using ZealousTTS.Common.Extensions;
using System.Reflection;


namespace ZealousTTS.Web.Utility
{
    public class BindSelectList
    {
        //public static SelectList Bind<T>(List<T> itemList, String dataValueField, String dataTextField)
        //    where T : class
        //{            
        //    SelectList selectList = new SelectList(itemList, dataValueField, dataTextField);
        //    SelectListItem item = new SelectListItem();            
        //    return selectList;            
        //}
        /// <summary>
        /// Binds a Collection to the Select List. Adds a new entry at first as (Please Select with blank value )
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="itemList"></param>
        /// <param name="dataValueField"></param>
        /// <param name="dataTextField"></param>
        /// <returns></returns>
        public static SelectList Bind<T>(List<T> itemList, String dataValueField, String dataTextField)
            where T : class
        {
            Type type = typeof(T);
            object obj = Activator.CreateInstance(type);
            PropertyInfo displayProperty = type.GetProperty(dataTextField);
            if (displayProperty == null)
                throw new ArgumentException(String.Format("Specified Data Text Field Property {0} was not found.", dataTextField));
            else
                displayProperty.SetValue(obj, "Please Select", null);

            displayProperty = type.GetProperty(dataValueField);
            if (displayProperty == null)
                throw new ArgumentException(String.Format("Specified Data Value Field Property {0} was not found.", dataValueField));
            else
                displayProperty.SetValue(obj, 0, null);

            if (itemList == null)
                itemList = new List<T>();

            itemList.Insert(0, (T)obj);
            SelectList selectList = new SelectList(itemList, dataValueField, dataTextField);
            return selectList;
        }

        //public static SelectList Manufacturers()
        //{
        //    AppEntities context = new AppEntities();
        //    List<Manufacturer> manufacturers = context.Manufacturers.OrderBy(P => P.Name).ToList();
        //    return Bind<Manufacturer>(manufacturers, "ID", "Name");
        //}
        /// <summary>
        /// Binds Job Categories
        /// </summary>
        /// <returns></returns>
        //public static SelectList Categories()
        //{
        //    ZealousTTSEntities context = new ZealousTTSEntities();
        //    List<Category> categories = (from P in context.Categories orderby P.Name select P).ToList();            
        //    return Bind<Category>(categories, "ID", "Name");
        //}
        //public static SelectList ParentCategories()
        //{
        //    AppEntities context = new AppEntities();
        //    List<Category> categories = (from P in context.Categories where P.ParentID == null select P).ToList(); 
            
        //    return Bind<Category>(categories, "ID", "Name");
        //}
        /// <summary>
        /// Binds Industry Types
        /// </summary>
        /// <returns></returns>
        //public static SelectList IndustryTypes()
        //{
        //    ZealousTTSEntities context = new ZealousTTSEntities();
        //    List<IndustryType> types = context.IndustryTypes.OrderBy(P => P.Name).ToList();
        //    return Bind<IndustryType>(types, "ID", "Name");
        //}
        /// <summary>
        /// Binds Countries
        /// </summary>
        /// <returns></returns>
        //public static SelectList Countries()
        //{
        //    ZealousTTSEntities context = new ZealousTTSEntities();
        //    List<Country> types = context.Countries.OrderBy(P => P.Name).ToList();
        //    return Bind<Country>(types, "ID", "Name");
        //}
        /// <summary>
        /// Binds States by Country
        /// </summary>
        /// <param name="countryID"></param>
        /// <returns></returns>
        //public static SelectList States(int countryID)
        //{
        //    ZealousTTSEntities context = new ZealousTTSEntities();
        //    List<State> types = (from P in context.States where P.CountryID == countryID orderby P.Name select P).ToList();
        //    return Bind<State>(types, "ID", "Name");
        //}
        /// <summary>
        /// Binds Cities By State
        /// </summary>
        /// <param name="stateID"></param>
        /// <returns></returns>
        //public static SelectList CitiesByState(int stateID)
        //{
        //    ZealousTTSEntities context = new ZealousTTSEntities();
        //    List<City> types = (from P in context.Cities where P.StateID == stateID orderby P.Name select P).ToList();
        //    return Bind<City>(types, "ID", "Name");
        //}
        /// <summary>
        /// Binds Cities By Country
        /// </summary>
        /// <param name="countryID"></param>
        /// <returns></returns>
        //public static SelectList CitiesByCountry(int countryID)
        //{
        //    ZealousTTSEntities context = new ZealousTTSEntities();
        //    //var states = from C in context.States where C.CountryID == countryID select C.ID;
        //    var query = from P in context.Cities
        //                where (from C in context.States where C.CountryID == countryID select C.ID).Contains(P.StateID)
        //                select P;
        //    List<City> types = new List<City>();
        //    if (query != null && query.Count() > 0)
        //        types = query.ToList();
        //    return Bind<City>(types, "ID", "Name");
        //}
        //public static SelectList Gender()
        //{
        //    var genderList = new SelectList(new[]
        //                    {
        //                        new {ID="",Name="Please Select"},
        //                        new {ID="M",Name="Male"},
        //                        new {ID="F",Name="Female"},
        //                        new {ID="O",Name="Other"},
        //                    }, "ID", "Name");  
        //    return genderList;
        //}

        //public static SelectList Religions()
        //{
        //    AppEntities context = new AppEntities();
        //    List<Religion> types = (from P in context.Religions orderby P.Name select P).ToList();
        //    return Bind<Religion>(types, "ID", "Name");
        //}

        //public static SelectList PreferredPhone()
        //{
        //    var phoneTypeList = new SelectList(new[]
        //                    {
        //                        new {ID="",Name="Please Select"},
        //                        new {ID="H",Name="Home"},
        //                        new {ID="M",Name="Mobile"},
        //                        new {ID="O",Name="Office"},
        //                    }, "ID", "Name");
        //    return phoneTypeList;
        //}
    }
}